<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of model_product
 *
 * @author b4by
 */
class model_product extends MY_Model {
	var $table = 'product';
	
	public function get_list($lang, $input=array(), $location_id=false , $cat_id=false)
	{
		if(!isset($input['select']))
			$input['select'] = 'id,price,sale_price,start,end,name,amount,thumb,active,voucher,amount,total_orders';
		
		$input['where']['lang_id'] = $lang;
		
		$this->db->from('product p');
		$this->db->join('product_info i','p.id=i.product_id','left');

		if($location_id!==false){
            $this->db->join('product_location l','p.id=l.product_id','left');
            $input['where']['location_id'] = $location_id;
        }

        if($cat_id!==false)
        {
            $this->db->join('product_catalogue c','p.id=c.product_id','left');
            $input['where']['cat_id'] = $cat_id;
        }

        $this->get_list_set_input($input);
		
		$query = $this->db->get();
		$products = $query->result();
		foreach($products as &$product)
		{
			$a = $product->voucher==1?$product->sale_price:$product->price-$product->sale_price;
			$product->save = (int)($a/$product->price*100);
		}
		return $products;
	}
	
	public function get_info($id)
	{
		$data = parent::get_info($id);
		if($data===FALSE) return FALSE;
		
		$this->load->helper('date');
		if($data->end <= now()) $data->active=0;
		
		$this->load->model('model_product_info');
		$info = $this->model_product_info->get_list(array(
			'where'		=>array('product_id' => $id),
			'order'	=> array('lang_id','asc'),
		));
		foreach($info as $row)
			$data->info[$row->lang_id] = $row;
		
		$this->load->model('model_product_catalogue');
		$data->catalogues = $this->model_product_catalogue->get_list(array(
            'select'    => 'cat_id',
			'where'		=> array('product_id' => $id),
			'order'	=> array('cat_id','asc'),
		));
		$this->load->model('model_product_location');
		$data->locations = $this->model_product_location->get_list(array(
            'select'    => 'location_id',
			'where'		=> array('product_id' => $id),
			'order'	=> array('location_id','asc'),
		));
		$this->load->model('model_product_image');
        $data->images = $this->model_product_image->get_list(array(
            'where'     => array('product_id'   => $id)
        ));
		
		$this->load->model('model_order');
		$data->orders = $this->model_order->get_list(array(
			'where'	=> array('product_id'	=> $id)
		));
		
		$a = $data->voucher==1?$data->sale_price:$data->price-$data->sale_price;
		$data->save = (int)($a/$data->price*100);
		return $data;
		
	}

	public function get_total($where = array()) {
		$this->db->from('product p');
		
		if(isset($where['l.location_id'])) $this->db->join('product_location l','p.id = l.product_id','left');
		if(isset($where['c.cat_id'])) $this->db->join('product_catalogue c','p.id = c.product_id','left');
		//$this->db->group_by('p.id');
		$this->db->where($where);
		return $this->db->count_all_results();
	}
}

?>
